package com.zs.zcerp.approval.controller;

import com.zs.zcerp.approval.entity.*;
import com.zs.zcerp.approval.service.IMailService;
import com.zs.zcerp.approval.service.IQhChkmgrdService;
import com.zs.zcerp.approval.service.IQhChkmgrmService;
import com.zs.zcerp.common.annotation.ControllerEndpoint;
import com.zs.zcerp.common.controller.BaseController;
import com.zs.zcerp.common.entity.FebsResponse;
import com.zs.zcerp.common.entity.QueryRequest;
import com.zs.zcerp.common.entity.Strings;
import com.zs.zcerp.common.exception.FebsException;
import com.zs.zcerp.common.utils.ConstantUtil;
import com.zs.zcerp.system.service.IUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 签核
 *
 * @author Henry
 * @email *****@mail.com
 * @date 2021-04-09 20:58:44
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping("approvals")
public class QhChkmgrdController extends BaseController {

    private final IQhChkmgrdService iQhChkmgrdService;
    private final IQhChkmgrmService iQhChkmgrmService;
    private final IUserService userService;
    private final IMailService mailService;

    @GetMapping("signDetails")
    @RequiresPermissions("approvals:view")
    public FebsResponse getUser(@NotBlank(message = "{required}") String orderNo) {
        //签核人数据
        List<QhChkmgrdEntity> qhChkmgrdList = iQhChkmgrdService.findSignList(orderNo);
        return new FebsResponse().success().data(qhChkmgrdList);
    }

    @GetMapping("detail")
    @RequiresPermissions("approvals:view")
    public FebsResponse getDetail(@NotBlank(message = "{required}") String orderNo, String itemType, String orgId) {
        //签核人数据
        List<SignDetail> signDetail = iQhChkmgrdService.selectDetail(orderNo.trim(), itemType, orgId.trim());
        return new FebsResponse().success().data(signDetail);
    }


    @GetMapping("list")
    @RequiresPermissions("approvals:view")
    public FebsResponse aprovalsList(QhChkmgrdEntity qhChkmgrdEntity, QueryRequest request) {
        if (qhChkmgrdEntity.getOrderNo() != null) {
            qhChkmgrdEntity.setOrderNo(qhChkmgrdEntity.getOrderNo().trim());
        }
        if (qhChkmgrdEntity.getGrtUser() != null) {
            qhChkmgrdEntity.setGrtUser(qhChkmgrdEntity.getGrtUser().trim());
        }

        qhChkmgrdEntity.setToUser(getCurrentUser().getUsername());
        return new FebsResponse().success()
                .data(getDataTable(iQhChkmgrdService.findApprovalsDetailList(qhChkmgrdEntity, request)));
    }


  /*  @PostMapping("batchSign")  注释方法保存
    @RequiresPermissions("aapprovals:batchSign")
    @ControllerEndpoint(operation = "批量签核单据", exceptionMessage = "批量签核单据失败")
    @ResponseBody
    public FebsResponse updateaAprovals(@RequestBody List<QhChkmgrdEntity> qhChkmgrdEntityList) {
        if (qhChkmgrdEntityList.size() < 0) {
            throw new FebsException(" 更新签核人员数据为空");
        }
        //iQhBasestdDService.bacthUpdate(qhChkmgrdEntityList);
        return new FebsResponse().success();
    }*/


    public HtmlVO mailHtml(EmpEntity e) {
        HtmlVO htmlVO = new HtmlVO();
        htmlVO.setToEmail(e.getToMail());
        htmlVO.setContent(
                "<!DOCTYPE html>" +
                        "<html lang='en'>" +
                        " <head></head> \n" +
                        " <body> " +
                        "  <font size='4' color='black'></font> \n" +
                        "  <p align='left'><font>各位同仁:<br />&nbsp;&nbsp;大家好！<br />&nbsp;&nbsp;以下销毁单据已送签至各位，请及时完成单据审核:</font> </p> \n" +
                        "  <font size='4' face='标楷体' color='black'></font> \n" +
                        "  <table border='1' cellspacing='0' cellspace='0'> \n" +
                        "   <tbody> \n" +
                        "    <tr align='left'> \n" +
                        "     <td>组织</td> \n" +
                        "     <td>销毁单号</td> \n" +
                        "     <td>送签人</td> \n" +
                        "     <td>送签时间</td> \n" +
                        "     <td>上级签核人</td> \n" +
                        "     <td>上级签核时间</td> \n" +
                        "    </tr> \n" +
                        "    <tr align='center'> \n" +
                        "     <td nowrap=''>" + e.getOrgName() + "</td> \n" +
                        "     <td nowrap=''>" + e.getOrderNo() + "</td> \n" +
                        "     <td nowrap=''>" + e.getGrtUserCname() + "</td> \n" +
                        "     <td nowrap=''>" + e.getSendDate() + "</td> \n" +
                        "     <td nowrap=''>" + e.getNextUser() + "</td> \n" +
                        "     <td nowrap=''>" + e.getChkDate() + "</td> \n" +
                        "    </tr> \n" +
                        "   </tbody>" +
                        "</table> \n" +
                        "  <bottom> \n" +
                        "   <font size='4' color='black'><u></u><p align='left'><u>签核网址:<a href='http://192.168.33.28:8000/'>http://192.168.33.28:8000/</a> <br /></u><font size='4' color='black'><br />Best Regards<br />总公司品牌保护-何三华<br />TEL:674686<br />2021/05/15</font></p></font> \n" +
                        "  </bottom>  \n" +
                        " </body>\n" +
                        "</html>"
        );
        htmlVO.setSubject("单据签核");
        return htmlVO;
    }

    @PostMapping("batchSign")
    @RequiresPermissions("approvals:sign")
    @ControllerEndpoint(operation = "签核批量单据", exceptionMessage = "签核批量单据失败")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public FebsResponse batchSign(@RequestBody List<QhChkmgrdEntity> qhChkmgrdlist) throws ParseException {

        if (CollectionUtils.isEmpty(qhChkmgrdlist)) {
            return new FebsResponse().message("签核数据为空");
        }
            for (QhChkmgrdEntity qhChkmgrdEntity : qhChkmgrdlist) {

                qhChkmgrdEntity.setChkOk(qhChkmgrdEntity.getChkOk()!=null?qhChkmgrdEntity.getChkOk():"同意");
                //batchSign
                if (qhChkmgrdEntity.getChkmgrId() == null) {
                    throw new FebsException("流程编码不能空");
                }
                String currentUser = getCurrentUser().getUsername();
                String currentAprovaltUser = qhChkmgrdEntity.getToUser();
                qhChkmgrdEntity.setCurrentUser(currentAprovaltUser);
                String billstdNo = qhChkmgrdEntity.getBillstdNo();
                /** 为最后节点*/
                String strY = "Y";
                /** 为中间节点*/
                String strN = "N";

                /***1、判断是否为当前用户   2、是否为不是最后节点
                 * 类型 销毁(DESTORY)   样品（DESTORY_Y）
                 * */
                if (qhChkmgrdEntity.getChkStatus().equals(1) && currentUser.equals(currentAprovaltUser)
                        && strN.equals(qhChkmgrdEntity.getIsLast())) {
                    //流程QH_CHKMGR_D 表签核
                    iQhChkmgrdService.updateaAprovals(qhChkmgrdEntity);
                    EmpEntity empEntity = userService.findUserNameAndMail(qhChkmgrdEntity.getNextUser());

                    if (null != empEntity && "".equals(empEntity.getToMail())) {
                        return new FebsResponse().message("用户基础数据为空 邮件为空");
                    }
                    empEntity.setOrgName(qhChkmgrdEntity.getOrgName());
                    empEntity.setGrtUserCname(qhChkmgrdEntity.getGrtUserCname());
                    empEntity.setOrderNo(qhChkmgrdEntity.getOrderNo());
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    empEntity.setSendDate(sdf.format(qhChkmgrdEntity.getSendDate()));
                    empEntity.setChkDate(sdf.format(new Date()));
                    empEntity.setNextUser(qhChkmgrdEntity.getToUserCname());

                    /**
                     *获取多人邮件 for 发送
                     */
                    String[] toMails = StringUtils.split(empEntity.getToMail(), Strings.SEMICOLON);
                    if(toMails!=null&&!"".equals(toMails)){
                        List<String> list = Arrays.asList(toMails);
                        HtmlVO htmlVO = mailHtml(empEntity);
                        htmlVO.setSubject("提醒：您有如下BC品销毁申请单待签核(截止" + sdf.format(qhChkmgrdEntity.getSendDate()) + ")");
                        for (int i = 0; i < list.size(); i++) {
                            mailService.sendHtmlMail(list.get(i), htmlVO.getSubject(), htmlVO.getContent());
                        }
                    }

                    continue;
                }
                if ("DESTORY".equals(qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
                    iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
                    iQhChkmgrdService.destorySignStatus(qhChkmgrdEntity.getOrderNo());
                    continue;
                }

                if ("DESTORY_Y".equals(qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
                    iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
                    iQhChkmgrmService.yqSignStatus(qhChkmgrdEntity.getOrderNo());
                    continue;
                }
                if (inContains(ConstantUtil.GAPOOrder, qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
                    iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
                    iQhChkmgrmService.GAPOSignStatus(qhChkmgrdEntity.getOrderNo(), ConstantUtil.orderEffective);
                    continue;
                }
                if (inContains(ConstantUtil.MDPOOrder, qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
                    iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
                    iQhChkmgrmService.MDPOSignStatus(qhChkmgrdEntity.getOrderNo(), ConstantUtil.orderEffective);
                    continue;
                }
                if (inContains(ConstantUtil.pomOrder, qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
                    iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
                    iQhChkmgrmService.PO6SignStatus(qhChkmgrdEntity.getOrderNo(), ConstantUtil.orderEffective);
                    continue;
                }
                if (inContains(ConstantUtil.PGOrder, qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
                    iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
                    iQhChkmgrmService.PGSignStatus(qhChkmgrdEntity.getOrderNo(), ConstantUtil.orderEffective);
                    continue;
                }
                if (inContains(ConstantUtil.kfOrder, qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
                    iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
                    iQhChkmgrmService.KFPSignStatus(qhChkmgrdEntity.getOrderNo(), ConstantUtil.orderEffective);
                    continue;
                }
                if (inContains(ConstantUtil.woOrder, qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
                    iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
                    iQhChkmgrmService.WOSignStatus(qhChkmgrdEntity.getOrderNo(), ConstantUtil.orderEffective);
                    continue;
                }

                if (inContains(ConstantUtil.FATRorder, qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
                    iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
                    iQhChkmgrmService.FATRSignStatus(qhChkmgrdEntity.getOrderNo(), ConstantUtil.orderEffective);
                    continue;
                }
                if (inContains(ConstantUtil.FAINorder, qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
                    iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
                    iQhChkmgrmService.FAINSignStatus(qhChkmgrdEntity.getOrderNo(), ConstantUtil.orderEffective);
                    continue;
                }


                /** type19 补料采购单   补料总表转采购单 在pomOrder  */

                /** type21 加工变更单*/
                if (inContains(ConstantUtil.WPGOrder, qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
                    iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
                    /**2019.11.04 变更单无论通过或是退回都变为生效状态 联越ERP原备注*/
                    iQhChkmgrmService.WPGignStatus(qhChkmgrdEntity.getOrderNo(), ConstantUtil.orderEffective);
                    continue;
                }
                /**type24 底厂直接采购单/底厂采购单*/
                if (inContains(ConstantUtil.XPOOrder, qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
                    iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
                    iQhChkmgrmService.XPOSignStatus(qhChkmgrdEntity.getOrderNo(), ConstantUtil.orderEffective);
                    continue;
                }
                /** type26 采购变更单*/
                if (inContains(ConstantUtil.XPCOrder, qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
                    iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
                    iQhChkmgrmService.XPCSignStatus(qhChkmgrdEntity.getOrderNo(), ConstantUtil.orderEffective);
                    continue;
                }

            }

        return new FebsResponse().success();
    }

    @PostMapping("sign")
    @RequiresPermissions("approvals:sign")
    @ControllerEndpoint(operation = "签核单据", exceptionMessage = "签核单据失败")
    @ResponseBody
    public FebsResponse updateSign(@RequestBody QhChkmgrdEntity qhChkmgrdEntity) throws ParseException {
        //batchSign
        if (qhChkmgrdEntity.getChkmgrId() == null) {
            throw new FebsException("流程编码不能空");
        }
        String currentUser = getCurrentUser().getUsername();
        String currentAprovaltUser = qhChkmgrdEntity.getToUser();
        qhChkmgrdEntity.setCurrentUser(currentAprovaltUser);
        String billstdNo = qhChkmgrdEntity.getBillstdNo();
        /** 为最后节点*/
        String strY = "Y";
        /** 为中间节点*/
        String strN = "N";

        /***1、判断是否为当前用户   2、是否为不是最后节点
         * 类型 销毁(DESTORY)   样品（DESTORY_Y）
         * */
        if (qhChkmgrdEntity.getChkStatus().equals(1) && currentUser.equals(currentAprovaltUser) && strN.equals(qhChkmgrdEntity.getIsLast())) {
            //流程QH_CHKMGR_D 表签核
            iQhChkmgrdService.updateaAprovals(qhChkmgrdEntity);
            EmpEntity empEntity = userService.findUserNameAndMail(qhChkmgrdEntity.getNextUser());

            if (null != empEntity && "".equals(empEntity.getToMail())) {
                return new FebsResponse().message("用户基础数据为空 邮件为空");
            }
            empEntity.setOrgName(qhChkmgrdEntity.getOrgName());
            empEntity.setGrtUserCname(qhChkmgrdEntity.getGrtUserCname());
            empEntity.setOrderNo(qhChkmgrdEntity.getOrderNo());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            empEntity.setSendDate(sdf.format(qhChkmgrdEntity.getSendDate()));
            empEntity.setChkDate(sdf.format(new Date()));
            empEntity.setNextUser(qhChkmgrdEntity.getToUserCname());

            /**
             *获取多人邮件 for 发送
             */
            String[] toMails = StringUtils.split(empEntity.getToMail(), Strings.SEMICOLON);
            List<String> list = Arrays.asList(toMails);
            HtmlVO htmlVO = mailHtml(empEntity);
            htmlVO.setSubject("提醒：您有如下BC品销毁申请单待签核(截止" + sdf.format(qhChkmgrdEntity.getSendDate()) + ")");
            for (int i = 0; i < list.size(); i++) {
                mailService.sendHtmlMail(list.get(i), htmlVO.getSubject(), htmlVO.getContent());
            }
            return new FebsResponse().success();
        } else if ("DESTORY".equals(qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
            iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
            iQhChkmgrdService.destorySignStatus(qhChkmgrdEntity.getOrderNo());

            return new FebsResponse().success();
        } else if ("DESTORY_Y".equals(qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
            iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
            iQhChkmgrmService.yqSignStatus(qhChkmgrdEntity.getOrderNo());
            return new FebsResponse().success();

        }
//        else if (inContains(ConstantUtil.kfOrder, qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
//            //签流程表
//            iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
//            //回签联越erp状态
//            String status = "7";
//            iQhChkmgrmService.orderSignStatus(qhChkmgrdEntity.getOrderNo(), status);
//        }
        if (inContains(ConstantUtil.GAPOOrder, qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
            iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
            iQhChkmgrmService.GAPOSignStatus(qhChkmgrdEntity.getOrderNo(), ConstantUtil.orderEffective);
            return new FebsResponse().success();
        }
        if (inContains(ConstantUtil.MDPOOrder, qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
            iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
            iQhChkmgrmService.MDPOSignStatus(qhChkmgrdEntity.getOrderNo(), ConstantUtil.orderEffective);
            return new FebsResponse().success();
        }
        if (inContains(ConstantUtil.pomOrder, qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
            iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
            iQhChkmgrmService.PO6SignStatus(qhChkmgrdEntity.getOrderNo(), ConstantUtil.orderEffective);
            return new FebsResponse().success();
        }
        if (inContains(ConstantUtil.PGOrder, qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
            iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
            iQhChkmgrmService.PGSignStatus(qhChkmgrdEntity.getOrderNo(), ConstantUtil.orderEffective);
            return new FebsResponse().success();
        }
        if (inContains(ConstantUtil.kfOrder, qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
            iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
            iQhChkmgrmService.KFPSignStatus(qhChkmgrdEntity.getOrderNo(), ConstantUtil.orderEffective);
            return new FebsResponse().success();
        }
        if (inContains(ConstantUtil.woOrder, qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
            iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
            iQhChkmgrmService.WOSignStatus(qhChkmgrdEntity.getOrderNo(), ConstantUtil.orderEffective);
            return new FebsResponse().success();
        }

        if (inContains(ConstantUtil.FATRorder, qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
            iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
            iQhChkmgrmService.FATRSignStatus(qhChkmgrdEntity.getOrderNo(), ConstantUtil.orderEffective);
            return new FebsResponse().success();
        }
        if (inContains(ConstantUtil.FAINorder, qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
            iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
            iQhChkmgrmService.FAINSignStatus(qhChkmgrdEntity.getOrderNo(), ConstantUtil.orderEffective);
            return new FebsResponse().success();
        }


        /** type19 补料采购单   补料总表转采购单 在pomOrder  */

        /** type21 加工变更单*/
        if (inContains(ConstantUtil.WPGOrder, qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
            iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
            /**2019.11.04 变更单无论通过或是退回都变为生效状态 联越ERP原备注*/
            iQhChkmgrmService.WPGignStatus(qhChkmgrdEntity.getOrderNo(), ConstantUtil.orderEffective);
            return new FebsResponse().success();
        }
        /**type24 底厂直接采购单/底厂采购单*/
        if (inContains(ConstantUtil.XPOOrder, qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
            iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
            iQhChkmgrmService.XPOSignStatus(qhChkmgrdEntity.getOrderNo(), ConstantUtil.orderEffective);
            return new FebsResponse().success();
        }
        /** type26 采购变更单*/
        if (inContains(ConstantUtil.XPCOrder, qhChkmgrdEntity.getBillstdNo()) && strY.equals(qhChkmgrdEntity.getIsLast())) {
            iQhChkmgrdService.updateaMainSign(qhChkmgrdEntity);
            iQhChkmgrmService.XPCSignStatus(qhChkmgrdEntity.getOrderNo(), ConstantUtil.orderEffective);
            return new FebsResponse().success();
        }
        return new FebsResponse().fail();
    }

    @PostMapping("report")
    @RequiresPermissions("approvals:view")
    @ControllerEndpoint(operation = "报表查看", exceptionMessage = "报表查看")
    @ResponseBody
    public FebsResponse getReport(@RequestBody QhChkmgrdEntity qhChkmgrdEntity) throws Exception {
        if (qhChkmgrdEntity.getOrderNo() == null) {
            throw new FebsException(" 订单编号不能空");
        }
        //签核人列表
        List<QhChkmgrdEntity> qhChkmgrdList = iQhChkmgrdService.findSignList(qhChkmgrdEntity.getOrderNo());

        //  报表22 固定资产异动单号
        if (ConstantUtil.inContains(ConstantUtil.FATRorder, qhChkmgrdEntity.getBillstdNo())) {
            //台头为空返回空
            OrderFatrEntity reportTitle = iQhChkmgrdService.findFatrOrderTitle(qhChkmgrdEntity);
            if (reportTitle == null) {
                return new FebsResponse().success().data(null);
            }
            //设置表头内容
            OrderFatrEntity drList = new OrderFatrEntity();
            drList.setOrgId(reportTitle.getOrgId());
            drList.setTitle(reportTitle.getTitle());
            drList.setOrgName(reportTitle.getOrgName());
            drList.setOutOrgName(reportTitle.getOutOrgName());
            drList.setOrderNo(reportTitle.getOrderNo());
            drList.setOrderNoDate(reportTitle.getOrderNoDate());
            drList.setChgType(reportTitle.getChgType());
            drList.setIntoOrgName(reportTitle.getIntoOrgName());
            drList.setType("22");

            //台头不空返内容
            List<OrderFatrEntity> reportDetails = iQhChkmgrdService.findFatrReport(qhChkmgrdEntity);
            drList.setDetailList(reportDetails);
            drList.setQhChkmgrdList(qhChkmgrdList);
            return new FebsResponse().success().data(drList);
        }
        //  报表23 固定资产转入单号
        if (ConstantUtil.inContains(ConstantUtil.FAINorder, qhChkmgrdEntity.getBillstdNo())) {
            // 查询报表
            List<OrderFatrEntity> reportDetails = iQhChkmgrdService.findFainReport(qhChkmgrdEntity);
            if (reportDetails != null && !reportDetails.isEmpty()) {
                //设置报表内容
                OrderFatrEntity drList = new OrderFatrEntity();
                drList.setType("23");
                drList.setDetailList(reportDetails);
                drList.setQhChkmgrdList(qhChkmgrdList);
                return new FebsResponse().success().data(drList);
            } else {
                return new FebsResponse().success().data(null);
            }
        }
        //type24 底厂直接采购单/ 25 底厂采购单
        if (ConstantUtil.inContains(ConstantUtil.XPOOrder, qhChkmgrdEntity.getBillstdNo())) {
            OrderXPOEntity reportTitle = iQhChkmgrdService.findXPOOrderTitle(qhChkmgrdEntity);
            if (reportTitle != null) {
                if ("6".equals(reportTitle.getSrc())) {
                    reportTitle.setType("24");
                } else if ("2".equals(reportTitle.getSrc())) {
                    reportTitle.setType("25");
                }
                List<OrderXPOEntity> reportDetails = iQhChkmgrdService.findXPOReport(qhChkmgrdEntity);
                if (reportDetails != null && !reportDetails.isEmpty()) {
                    reportTitle.setDetailList(reportDetails);
                    reportTitle.setQhChkmgrdList(qhChkmgrdList);
                } else {
                    return new FebsResponse().success().data(reportDetails);
                }
            }
            return new FebsResponse().success().data(reportTitle);
        }

        if (ConstantUtil.inContains(ConstantUtil.XPCOrder, qhChkmgrdEntity.getBillstdNo())) {
            OrderXPCEntity reportTitle = iQhChkmgrdService.findXPCOrderTitle(qhChkmgrdEntity);
            if (reportTitle != null) {
                reportTitle.setType("26");
                qhChkmgrdEntity.setOrderNo(reportTitle.getChgId());
                List<OrderXPCEntity> reportDetails = iQhChkmgrdService.findXPCReport(qhChkmgrdEntity);
                if (reportDetails != null && !reportDetails.isEmpty()) {
                    reportTitle.setDetailList(reportDetails);
                    reportTitle.setQhChkmgrdList(qhChkmgrdList);
                } else {
                    return new FebsResponse().success().data(reportDetails);
                }
            }
            return new FebsResponse().success().data(reportTitle);
        }
        // henry  2021 08 按分类查详情
        if (ConstantUtil.DESTORY.equals(qhChkmgrdEntity.getBillstdNo()) || ConstantUtil.DESTORY_Y.equals(qhChkmgrdEntity.getBillstdNo())) {

            // ----- 报表1  部位（标准报表)    DESTORY_TYPE = YE_Y
            List<DestoryReportEntity> reportList = iQhChkmgrdService.findListReport(qhChkmgrdEntity);
            String destoryType = reportList.get(0).getDestoryType();
            String brandNo = reportList.get(0).getBrandNo();
            if (!reportList.isEmpty() && null != reportList && "SE_Y".equals(destoryType) && (brandNo == null || "".equals(brandNo))) {
                if (reportList.size() < 0) {
                    DestoryReportEntity drList = null;
                    drList.setType("1");
                    return new FebsResponse().success().data(drList);
                }
                DestoryReportEntity drList = new DestoryReportEntity();
                /**
                 * 设置表报分类  */
                drList.setType("1");
                drList.setWcName(reportList.get(0).getWcName());
                drList.setDestoryNo(reportList.get(0).getDestoryNo());
                drList.setUserNm(reportList.get(0).getUserNm());
                drList.setNote(reportList.get(0).getNote());
                drList.setDestoryDate(reportList.get(0).getDestoryDate());
                drList.setOrgName(reportList.get(0).getOrgName());

                int leftAmountsTemp = 0;
                int reftAmountsTemp = 0;
                int leftQty = 0;
                int rightQty = 0;
                List<DestoryDetailEntity> detailEntityList = new ArrayList<DestoryDetailEntity>();
                for (int i = 0; i < reportList.size(); i++) {
                    DestoryReportEntity list = reportList.get(i);
                    DestoryDetailEntity detailEntity = new DestoryDetailEntity();
                    detailEntity.setPartNm(list.getPartNm());
                    detailEntity.setSeqitem(list.getSeqitem());
                    detailEntity.setBoxXh(list.getBoxXh());
                    detailEntity.setDeptName(list.getDeptName());
                    detailEntity.setDestoryLqty(list.getDestoryLqty());
                    detailEntity.setDestoryRqty(list.getDestoryRqty());
                    detailEntity.setNoteD(list.getNoteD());

                    if (list.getDestoryLqty() != null) {
                        leftQty = leftQty + list.getDestoryLqty();
                    }
                    if (list.getDestoryRqty() != null) {
                        rightQty = rightQty + list.getDestoryRqty();
                    }
                    detailEntityList.add(detailEntity);

                    int x = 1;
                    String currentPartNo = reportList.get(i).getPartNo();
                    String backPartNo = currentPartNo;
                    //获取上个对应编码防止角标越界
                    if (i + 1 == reportList.size()) {
                        backPartNo = currentPartNo;
                    } else {
                        backPartNo = reportList.get(i + x).getPartNo();
                    }

                    //判断第一个和第二个 部位编码是否相同
                    if (!currentPartNo.equals(backPartNo) || i + 1 == reportList.size()) {
                        DestoryDetailEntity detailEntityTotal = new DestoryDetailEntity();
                        //合计
                        detailEntityTotal.setLeftTotal(leftQty);
                        detailEntityTotal.setRightTotal(rightQty);
                        detailEntityList.add(detailEntityTotal);
                        //总计
                        leftAmountsTemp = leftQty;
                        reftAmountsTemp = rightQty;
                        drList.setLeftAmounts(leftAmountsTemp + drList.getLeftAmounts());
                        drList.setRightAmounts(reftAmountsTemp + drList.getRightAmounts());
                        leftQty = 0;
                        rightQty = 0;
                    }
                }
                //签核人数据
                drList.setDetailList(detailEntityList);
                drList.setQhChkmgrdList(qhChkmgrdList);
                return new FebsResponse().success()
                        .data(drList);
            }

            // 报表2 AD 报表        DESTORY_TYPE = YE_Y BRAND_NO=AD
            List<ADReportEntity> adTitleEntity = iQhChkmgrdService.findAdReportTitle(qhChkmgrdEntity);
            if (!adTitleEntity.isEmpty() && null != adTitleEntity && "SE_Y".equals(adTitleEntity.get(0).getDestoryType()) && "AD".equals(adTitleEntity.get(0).getBrandNo())) {
                List<ADReportEntity> AdTempList = iQhChkmgrdService.findAdReport(qhChkmgrdEntity);

                if (AdTempList.size() < 0) {
                    ADReportEntity adList = null;
                    adList.setType("2");
                    return new FebsResponse().success().data(adList);
                }
                ADReportEntity adList = new ADReportEntity();
                //设置表报分类
                adList.setType("2");
                Integer orgId = adTitleEntity.get(0).getOrgId();
                String orgName = adTitleEntity.get(0).getOrgName();
                adList.setOrgId(orgId);
                adList.setOrgName(orgName);
                adList.setWcName(adTitleEntity.get(0).getWcName());
                adList.setLastUserNm(adTitleEntity.get(0).getLastUserNm());
                adList.setNote(adTitleEntity.get(0).getNote());
                adList.setDestoryNo(adTitleEntity.get(0).getDestoryNo());
                adList.setDestoryDate(adTitleEntity.get(0).getDestoryDate());
                adList.setTitle(adTitleEntity.get(0).getTitle());

                int destoryQtyAmountsTemp = 0;
                int destoryQty = 0;
                List<ADReportEntity> detailEntityList = new ArrayList<ADReportEntity>();
                for (int i = 0; i < AdTempList.size(); i++) {
                    ADReportEntity list = AdTempList.get(i);
                    ADReportEntity detailEntity = new ADReportEntity();
                    detailEntity.setBrandNm(list.getBrandNm());
                    detailEntity.setWcNameContent(list.getWcNameContent());
                    detailEntity.setLeaderNm(list.getLeaderNm());
                    detailEntity.setPo(list.getPo());
                    detailEntity.setPartNm(list.getPartNm());
                    detailEntity.setSeId(list.getSeId());
                    detailEntity.setArticNo(list.getArticNo());
                    detailEntity.setArticNM(list.getArticNM());
                    detailEntity.setDestoryQty(list.getDestoryQty());
                    detailEntity.setBoxXh(list.getBoxXh());
                    detailEntity.setNodeD(list.getNodeD());

                    if (list.getDestoryQty() != null) {
                        destoryQty = destoryQty + list.getDestoryQty();
                    }
                    detailEntityList.add(detailEntity);

                    int x = 1;
                    String currentBoxXh = AdTempList.get(i).getBoxXh();
                    if (currentBoxXh == null || currentBoxXh.equals("")) {
                        ADReportEntity detailEntityTotal = new ADReportEntity();
                        //合计
                        detailEntityTotal.setDestoryQty(destoryQty);
                        detailEntityList.add(detailEntityTotal);
                        //总计
                        destoryQtyAmountsTemp = destoryQty;
                        adList.setDestoryQtyAmounts(destoryQtyAmountsTemp + adList.getDestoryQtyAmounts());
                        destoryQty = 0;
                    } else {
                        String backBoxXh = currentBoxXh;
                        //获取上个对应编码防止角标越界
                        if (i + 1 == AdTempList.size()) {
                            backBoxXh = currentBoxXh;
                        } else {
                            backBoxXh = AdTempList.get(i + x).getBoxXh();
                        }
                        //判断第一个和第二个 部位编码是否相同
                        if (!currentBoxXh.equals(backBoxXh) || i + 1 == AdTempList.size()) {
                            ADReportEntity detailEntityTotal = new ADReportEntity();
                            //合计
                            detailEntityTotal.setDestoryQty(destoryQty);
                            detailEntityList.add(detailEntityTotal);
                            //总计
                            destoryQtyAmountsTemp = destoryQty;
                            adList.setDestoryQtyAmounts(destoryQtyAmountsTemp + adList.getDestoryQtyAmounts());
                            destoryQty = 0;
                        }
                    }
                }
                //签核人数据
                adList.setDetailList(detailEntityList);
                adList.setQhChkmgrdList(qhChkmgrdList);
                return new FebsResponse().success()
                        .data(adList);
            }
            // 报表3 材料表报   DESTORY_TYPE = SE_N
            SeReportEntity seTitleEntity = iQhChkmgrdService.findSeReportTitle(qhChkmgrdEntity);
            String sebrandNo = seTitleEntity.getBrandNo();
            if (seTitleEntity != null && null != seTitleEntity && "SE_N".equals(seTitleEntity.getDestoryType()) && (sebrandNo == null || "".equals(sebrandNo))) {
                List<SeReportEntity> seTempList = iQhChkmgrdService.findSeReport(qhChkmgrdEntity);
                if (seTempList.isEmpty()) {
                    SeReportEntity seReportEntity = null;
                    seReportEntity.setType("3");
                    return new FebsResponse().success().data(seReportEntity);
                }
                seTitleEntity.setType("3");
                seTitleEntity.setWcName(seTempList.get(0).getDeptName());
                //签核人数据
                seTitleEntity.setDetailList(seTempList);
                seTitleEntity.setQhChkmgrdList(qhChkmgrdList);
                return new FebsResponse().success().data(seTitleEntity);
            }
        }
        // 总务采购单
        if (inContains(ConstantUtil.GAPOOrder, qhChkmgrdEntity.getBillstdNo())) {
            GAPOReportEntity gapoReport = iQhChkmgrdService.findGAPOTitle(qhChkmgrdEntity);
            if (gapoReport != null) {
                gapoReport.setType("13");
                List<GAPOReportEntity> gapoReportList = iQhChkmgrdService.findGAPOReport(qhChkmgrdEntity);
                gapoReport.setDetailList(gapoReportList);
                gapoReport.setQhChkmgrdList(qhChkmgrdList);
            }
            return new FebsResponse().success()
                    .data(gapoReport);
        }
        // 模具采购单
        if (inContains(ConstantUtil.MDPOOrder, qhChkmgrdEntity.getBillstdNo())) {
            MDPOReportEntity mdpoReport = iQhChkmgrdService.findMDPOTitle(qhChkmgrdEntity);
            if (mdpoReport != null) {
                mdpoReport.setType("14");
                List<MDPOReportEntity> mdpoReportList = iQhChkmgrdService.findMDPOReport(qhChkmgrdEntity);
                mdpoReport.setDetailList(mdpoReportList);
                mdpoReport.setQhChkmgrdList(qhChkmgrdList);
            }
            return new FebsResponse().success()
                    .data(mdpoReport);
        }
        // 原料采购单/总表转采购单/直接采购单
        if (inContains(ConstantUtil.pomOrder, qhChkmgrdEntity.getBillstdNo())) {
            //补料采购单
            OrderBPOTitleEntity bpo19Title = iQhChkmgrdService.findBPO19Title(qhChkmgrdEntity);
            if (bpo19Title != null) {
                bpo19Title.setType("19");
                List<OrderBPODetailEntity> bpoDetailList = iQhChkmgrdService.findBPODetail(qhChkmgrdEntity);
                bpo19Title.setDetailList(bpoDetailList);
                bpo19Title.setQhChkmgrdList(qhChkmgrdList);
                return new FebsResponse().success()
                        .data(bpo19Title);
            }
            //补料总表转采购单
            OrderBPOTitleEntity bpo20Title = iQhChkmgrdService.findBPO20Title(qhChkmgrdEntity);
            if (bpo20Title != null) {
                bpo20Title.setType("20");
                List<OrderBPODetailEntity> bpoDetailList = iQhChkmgrdService.findBPODetail(qhChkmgrdEntity);
                bpo20Title.setDetailList(bpoDetailList);
                bpo20Title.setQhChkmgrdList(qhChkmgrdList);
                return new FebsResponse().success()
                        .data(bpo20Title);
            }

            PO6ReportEntity popoReport = iQhChkmgrdService.findPO6Title(qhChkmgrdEntity);
            if (popoReport != null) {
                popoReport.setType("6");
                List<PO6ReportEntity> popoReportList = iQhChkmgrdService.findPO6Report(qhChkmgrdEntity);
                popoReport.setDetailList(popoReportList);
                popoReport.setQhChkmgrdList(qhChkmgrdList);
                return new FebsResponse().success()
                        .data(popoReport);
            }
        }
        // 采购变更单
        if (inContains(ConstantUtil.PGOrder, qhChkmgrdEntity.getBillstdNo())) {
            PGReportEntity po6Report = iQhChkmgrdService.findPGTitle(qhChkmgrdEntity);
            if (po6Report != null) {
                po6Report.setType("8");
                List<PGReportEntity> pgReportList = iQhChkmgrdService.findPGReport(qhChkmgrdEntity);
                po6Report.setDetailList(pgReportList);
                po6Report.setQhChkmgrdList(qhChkmgrdList);
            }
            return new FebsResponse().success()
                    .data(po6Report);
        }
        // 开发采购单
        if (inContains(ConstantUtil.KFPOOrder, qhChkmgrdEntity.getBillstdNo())) {
            KFPOReportEntity KFPOReportEntity = iQhChkmgrdService.findKFPO2Title(qhChkmgrdEntity);
            if (KFPOReportEntity != null) {
                List<KFPOReportEntity> KFPOReport;
                if (KFPOReportEntity.getSrc() == 3 || KFPOReportEntity.getSrc() == 4) {
                    KFPOReportEntity.setType("12");
                    KFPOReport = iQhChkmgrdService.findKFPO2Report(qhChkmgrdEntity);
                } else {
                    KFPOReportEntity.setType("10");
                    KFPOReport = iQhChkmgrdService.findKFPOReport(qhChkmgrdEntity);
                }

                KFPOReportEntity.setDetailList(KFPOReport);
                KFPOReportEntity.setQhChkmgrdList(qhChkmgrdList);
            }
            return new FebsResponse().success()
                    .data(KFPOReportEntity);
        }

        // 开发加工采购单
        if (inContains(ConstantUtil.KFPROrder, qhChkmgrdEntity.getBillstdNo())) {
            KFPRReportEntity kfprReportEntity = iQhChkmgrdService.findKFPRTitle(qhChkmgrdEntity);
            if (kfprReportEntity != null) {
                kfprReportEntity.setType("11");
                List<KFPRReportEntity> kfprReport = iQhChkmgrdService.findKFPRReport(qhChkmgrdEntity);
                kfprReportEntity.setDetailList(kfprReport);
                kfprReportEntity.setQhChkmgrdList(qhChkmgrdList);
            }
            return new FebsResponse().success()
                    .data(kfprReportEntity);
        }

        // WO加工采购单
        if (inContains(ConstantUtil.woOrder, qhChkmgrdEntity.getBillstdNo())) {
            WOReportEntity woReportEntity = iQhChkmgrdService.findWOTitle(qhChkmgrdEntity);
            if (woReportEntity != null) {
                if (woReportEntity.getSrc() == 7) {
                    woReportEntity.setType("15");
                } else if (woReportEntity.getSrc() == 1) {
                    woReportEntity.setType("16");
                } else if (woReportEntity.getSrc() == 8) {
                    woReportEntity.setType("17");
                } else if (woReportEntity.getSrc() == 5) {
                    woReportEntity.setType("18");
                }
                List<WOReportEntity> woReport = iQhChkmgrdService.findWOReport(qhChkmgrdEntity);
                woReportEntity.setDetailList(woReport);
                woReportEntity.setQhChkmgrdList(qhChkmgrdList);
            }
            return new FebsResponse().success()
                    .data(woReportEntity);
        }


        /**
         * 判断报表类型   LEADER_NO
         *
         * 1  （部位报表)  标准    DESTORY_TYPE = YE_Y
         * 2  AD 报表        DESTORY_TYPE = YE_Y BRAND_NO=AD
         * 3 材料表报   DESTORY_TYPE = SE_N
         */
        return new FebsResponse().success()
                .data(null);

    }

    /**
     * 判断String数组中是否包含某个字符串
     *
     * @param source    如：String source = "aa,bb,cc,dd";
     * @param condition aa
     * @return ture
     */
    private static boolean inContains(String source, String condition) {
        String str = source;
        String[] strs = str.split(",");
        List<String> list = Arrays.asList(strs);//将String数组转化为list集合
        return list.contains(condition);
    }

}
